package com.sgb.kjwl.view.ui.fragment.manage;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FrSpecialAssetsForWebsiteBinding;
import com.sgb.kjwl.databinding.FrSpecialQualificationForWebsiteBinding;
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageFragComponent;
import com.sgb.kjwl.di.manage.EnterpriseManageFragModule;
import com.sgb.kjwl.model.entity.manage.CompanyAssetsEntity;
import com.sgb.kjwl.model.entity.manage.EpAssestEntity;
import com.sgb.kjwl.model.entity.manage.SpecialQualificationEntity;
import com.sgb.kjwl.view.ui.adapter.manage.CompanyPropertyAllAdapter;
import com.sgb.kjwl.view.ui.adapter.manage.EpAssetAdapter;
import com.sgb.kjwl.view.ui.adapter.manage.SpecialQualificationAdapter1;
import com.sgb.kjwl.viewmodel.manage.SpecialAssetsForWebsiteModel;
import com.sgb.kjwl.viewmodel.manage.SpecialQualificationForWebsiteModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

/*
 * Author LiWei
 * Create Time 2021/4/23
 * Description 网站-专项资产
 * */
public class SpecialAssetsForWebsite extends AppLazyFragment {

    @Inject
    SpecialAssetsForWebsiteModel viewModel;
    private FrSpecialAssetsForWebsiteBinding binding;
    private CompanyPropertyAllAdapter adapter;

    private String websiteNo;
    private int websiteType;

    public static SpecialAssetsForWebsite newInstance(String websiteNo, int websiteType) {
        SpecialAssetsForWebsite fragment = new SpecialAssetsForWebsite();
        Bundle args = new Bundle();
        args.putString("websiteNo", websiteNo);
        args.putInt("websiteType", websiteType);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        super.setupFragmentComponent(appComponent);
        DaggerEnterpriseManageFragComponent.builder().appComponent(appComponent)
                .enterpriseManageFragModule(new EnterpriseManageFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fr_special_assets_for_website, container, false);
        if (getArguments() != null) {
            websiteNo = getArguments().getString("websiteNo");
            websiteType = getArguments().getInt("websiteType");
        }
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        adapter = new CompanyPropertyAllAdapter(getActivity(), null);
        binding.listView.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.listView.addItemDecoration(new RecyclerCustomDecoration(0,
                0,
                0,
                getResources().getDimension(R.dimen.dp10)));
        binding.listView.setAdapter(adapter);
        binding.listView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getSpecialAssetsList(websiteNo, websiteType, false);
            }

            @Override
            public void onLoadMore() {
                viewModel.getSpecialAssetsList(websiteNo, websiteType, true);
            }
        });
    }

    @Override
    protected void onLazyLoad() {
        viewModel.getSpecialAssetsList(websiteNo, websiteType, false);
    }

    public void showList(List<CompanyAssetsEntity> list, boolean isLoadMore, boolean isLast) {
        if (isLoadMore) {
            binding.listView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                adapter.addDatas(list);
            }
            binding.listView.setNoMore(isLast);
        } else {
            binding.listView.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                adapter.setDatas(list);
            } else {
                adapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }
}
